<?php namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\Tools;
use App\Http\Services\UserManager;
use App\Orders;
use App\Position;
use App\SetTopRecord;
use App\SetTopRule;
use App\User;
use App\Userinfo;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function buildOrder(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $ruleId      = $request->get("rule_id");
        $type        = $request->get("type");
        $adventType  = $request->get("advent_type");
        $postitionId = $request->get("position_id");

        $thatRule = SetTopRule::whereId($ruleId)->first();
        if(!$thatRule)
            return response()->json(["level"=>"error","message"=>"当前规则不存在"]);

//        $thatPosition = Position::whereId($postitionId)->first();
//        if(!$thatPosition)
//            return response()->json(["level"=>"error","message"=>"当前职位不存在"]);

        if(!is_null($type) && $type == "wechat"){
            $buildOrder = new Orders();
            $buildOrder->user_id = $user->id;
            $buildOrder->order_num = Tools::makeBillNum();
            $buildOrder->s_amount  = $thatRule->num;
            $buildOrder->save();

            $newSetTopRecord = new SetTopRecord();

            if($adventType == "position"){
                $newSetTopRecord->position_id = $postitionId;
            }
            if ($adventType=="resume"){
                $newSetTopRecord->resume_id= $postitionId;
            }
            else{
                $newSetTopRecord->advent_id   = $postitionId;
            }

            $newSetTopRecord->hours       = $thatRule->hours;
            $newSetTopRecord->num         = $thatRule->num;
            $newSetTopRecord->type        = "wechat";
            $newSetTopRecord->order_num   = $buildOrder->order_num;
            $newSetTopRecord->status      = 0;
            $newSetTopRecord->remark      = $adventType;
            $newSetTopRecord->save();
        }else{
            $userInfo = Userinfo::whereUserId($user->id)->first();
            if(!$userInfo)
                return response()->json(["level"=>"error","message"=>"当前用户信息不存在"]);

            if($thatRule->num > $userInfo->integral){
                return response()->json(["level"=>"error","message"=>"您的积分不足，请选择其他付款方式"]);
            }

            $buildOrder = new Orders();
            $buildOrder->user_id   = $user->id;
            $buildOrder->order_num = Tools::makeBillNum();
            $buildOrder->s_amount  = $thatRule->num;
            $buildOrder->pay_type  = "integral";
            $buildOrder->ispay     = 1;
            $buildOrder->paytime   = date("Y-m-d H:i:s",time());
            $buildOrder->save();

            $newSetTopRecord = new SetTopRecord();
            if($adventType == "position"){
                $newSetTopRecord->position_id = $postitionId;
            }else{
                $newSetTopRecord->advent_id   = $postitionId;
            }
            $newSetTopRecord->hours       = $thatRule->hours;
            $newSetTopRecord->num         = $thatRule->num;
            $newSetTopRecord->type        = "integral";
            $newSetTopRecord->order_num   = $buildOrder->order_num;
            $newSetTopRecord->status      = 1;
            $newSetTopRecord->remark      = $adventType;
            $newSetTopRecord->save();
        }


        return response()->json(["level"=>"success","message"=>"生成订单成功！","data"=>$buildOrder]);
    }
}